/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;
 
import dto.Tau;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import util.ConnectToMySql;

public class TauDAO {
       public static List<Tau> LayDanhSachTau() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
         List<Tau> list = new ArrayList<Tau>();
        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            try {
                CallableStatement cs = conn.prepareCall("select * from Tau ");
                  ResultSet rs = cs.executeQuery();
                if (rs != null) {
                     while (rs.next()) {
                     Tau my = new Tau();
                    my.setMatau(rs.getInt("matau"));
                    my.setTendoantau(rs.getString("tentau"));
                    my.setHangsanxuat(rs.getString("hangsanxuat"));
                    my.setNgayvanhanh(rs.getString("ngayvanhanh"));
                    my.setMaloaitau(rs.getInt("maloaitau"));
                    my.setSoluongtoa(rs.getInt("soluongtoa"));   
                    my.setTrangthai(rs.getInt("trangthai"));   
                     list.add(my);
                }
                }
            } catch (Exception ex) {
                System.out.println(ex);
                return null;
            }
        }
        
        return list;
    }
     public static Tau LayThongTinTau(String matau) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
        Tau nd = new Tau();

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            try {
                CallableStatement cs = conn.prepareCall("select * from Tau where matau=?");
                cs.setInt(1, Integer.parseInt(matau)); 
                ResultSet rs = cs.executeQuery();
                if (rs != null) {
                    if (rs.next() == true) {
                        Tau my = new Tau();
                        my.setMatau(rs.getInt("matau"));
                    my.setTendoantau(rs.getString("tentau"));
                    my.setHangsanxuat(rs.getString("hangsanxuat"));
                    my.setNgayvanhanh(rs.getString("ngayvanhanh"));
                    my.setMaloaitau(rs.getInt("maloaitau"));
                    my.setSoluongtoa(rs.getInt("soluongtoa"));   
                    my.setTrangthai(rs.getInt("trangthai")); 
                     nd= my;
                    } else {
                        nd = null;
                    }
                }
            } catch (Exception ex) {
                return null;
            }
        }
        return nd;
    }
     public static int SoLuongTau() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
        int kq = 0;
       List<Tau> list = null;
        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            try {
                CallableStatement cs = conn.prepareCall("select * from Tau");
                  ResultSet rs = cs.executeQuery();
                if (rs != null) {
                     while (rs.next()) {
                     Tau my = new Tau();
                      my.setMatau(rs.getInt("matau"));
                    my.setTendoantau(rs.getString("tentau"));
                    my.setHangsanxuat(rs.getString("hangsanxuat"));
                    my.setNgayvanhanh(rs.getString("ngayvanhanh"));
                    my.setMaloaitau(rs.getInt("maloaitau"));
                    my.setSoluongtoa(rs.getInt("soluongtoa"));   
                    my.setTrangthai(rs.getInt("trangthai"));  
                     list.add(my);
                }
                }
            } catch (Exception ex) {
                System.out.println(ex); 
            }
        }
        kq= list.size();
        return kq;
    }
     
    public static boolean ThemTau(Tau nd) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
       boolean result = false;

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            CallableStatement cs = conn.prepareCall("insert into Tau(tentau,hangsanxuat,ngayvanhanh,maloaitau,soluongtoa,trangthai)  values(?,?,?,?,?,?)");
           
            cs.setString(1, nd.getTendoantau());
            cs.setString(2, nd.getHangsanxuat());
            cs.setString(3, nd.getNgayvanhanh());
            cs.setInt(4, nd.getMaloaitau());
            cs.setInt(5, nd.getSoluongtoa()); 
            cs.setInt(6, nd.getTrangthai()); 

            int n = 0;
            try {
                n = cs.executeUpdate();
                if (n > 0) {
                    result = true;
                }
            } catch (Exception ex) {
                result = false;
            }
        }

        return result;
    }
    
    public static boolean CapNhatTau(Tau nd)throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
     boolean result = false;

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            CallableStatement cs = conn.prepareCall("Update tau Set  tentau=?,hangsanxuat=?,ngayvanhanh	=?,maloaitau=?,soluongtoa=?,trangthai=?   Where matau = ?");
         
            cs.setString(1, nd.getTendoantau());
            cs.setString(2, nd.getHangsanxuat());
            cs.setString(3, nd.getNgayvanhanh());
            cs.setInt(4, nd.getMaloaitau());
            cs.setInt(5, nd.getSoluongtoa()); 
            cs.setInt(6, nd.getTrangthai()); 
            cs.setInt(7, nd.getMatau()); 

            int n = 0;
            try {
                n = cs.executeUpdate();
                if (n > 0) {
                    result = true;
                }
            } catch (Exception ex) {
                result = false;
            }
        }

        return result;  
    }
    
    public static boolean XoaTau(String tenDangNhap)throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
       boolean result = false;

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            CallableStatement cs = conn.prepareCall("delete from tau where matau=?");
            Integer matau = Integer.parseInt(tenDangNhap);
            cs.setInt(1, matau);

            int n = 0;
            try {
                n = cs.executeUpdate();
                if (n > 0) {
                    result = true;
                }
            } catch (Exception ex) {
                result = false;
            }
        }

        return result;
    }
    
     
}
